package com.agan.system.service.impl;

import java.util.List;

import com.agan.common.core.utils.StringUtils;
import com.agan.system.param.ListDictParam;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.agan.common.security.utils.DictUtils;
import com.agan.system.api.domain.SysDictData;
import com.agan.system.mapper.SysDictDataMapper;
import com.agan.system.service.ISysDictDataService;

/**
 * 字典 业务层处理
 * 
 * @author agan
 */
@Service
@RequiredArgsConstructor
public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataMapper, SysDictData> implements ISysDictDataService
{
    private final SysDictDataMapper dictDataMapper;

    /**
     * 根据条件分页查询字典数据
     * 
     * @param param 字典数据信息
     * @return 字典数据集合信息
     */
    @Override
    public IPage<SysDictData> selectDictDataList(ListDictParam param)
    {
        IPage<SysDictData> page = new Page<>(param.getPageNum(), param.getPageSize());

        LambdaQueryWrapper<SysDictData> sysDictDataLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysDictDataLambdaQueryWrapper.eq(StringUtils.isNotEmpty(param.getDictType()), SysDictData::getDictType, param.getDictType());
        sysDictDataLambdaQueryWrapper.like(StringUtils.isNotEmpty(param.getDictLabel()), SysDictData::getDictLabel, param.getDictLabel());
        sysDictDataLambdaQueryWrapper.eq(StringUtils.isNotEmpty(param.getStatus()), SysDictData::getStatus, param.getStatus());
        sysDictDataLambdaQueryWrapper.orderByDesc(SysDictData::getDictSort);

        return dictDataMapper.selectPage(page, sysDictDataLambdaQueryWrapper);
    }

    /**
     * 根据字典类型和字典键值查询字典数据信息
     * 
     * @param dictType 字典类型
     * @param dictValue 字典键值
     * @return 字典标签
     */
    @Override
    public String selectDictLabel(String dictType, String dictValue)
    {
        return dictDataMapper.selectDictLabel(dictType, dictValue);
    }

    /**
     * 根据字典数据ID查询信息
     * 
     * @param dictCode 字典数据ID
     * @return 字典数据
     */
    @Override
    public SysDictData selectDictDataById(String dictCode)
    {
        return dictDataMapper.selectById(dictCode);
    }

    /**
     * 批量删除字典数据信息
     * 
     * @param dictCodes 需要删除的字典数据ID
     */
    @Override
    public void deleteDictDataByIds(String[] dictCodes)
    {
        for (String dictCode : dictCodes)
        {
            SysDictData data = selectDictDataById(dictCode);
            dictDataMapper.deleteById(dictCode);
            List<SysDictData> dictDataList = dictDataMapper.selectDictDataByType(data.getDictType());
            DictUtils.setDictCache(data.getDictType(), dictDataList);
        }
    }

    /**
     * 新增保存字典数据信息
     * 
     * @param data 字典数据信息
     * @return 结果
     */
    @Override
    public int insertDictData(SysDictData data)
    {
        int row = dictDataMapper.insertDictData(data);
        if (row > 0)
        {
            List<SysDictData> dictDataList = dictDataMapper.selectDictDataByType(data.getDictType());
            DictUtils.setDictCache(data.getDictType(), dictDataList);
        }
        return row;
    }

    /**
     * 修改保存字典数据信息
     * 
     * @param data 字典数据信息
     * @return 结果
     */
    @Override
    public int updateDictData(SysDictData data)
    {
        int row = dictDataMapper.updateDictData(data);
        if (row > 0)
        {
            List<SysDictData> dictDataList = dictDataMapper.selectDictDataByType(data.getDictType());
            DictUtils.setDictCache(data.getDictType(), dictDataList);
        }
        return row;
    }
}
